package cn.bandeqing.satoken;

import cn.bandeqing.base.resp.Resp;
import cn.bandeqing.stp.StpAdminUtil;
import cn.bandeqing.stp.StpWechatMiniAppUtil;
import cn.bandeqing.stp.StpWorkerUtil;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.reactor.filter.SaReactorFilter;
import cn.dev33.satoken.router.SaRouter;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.http.meta.HeaderName;
import org.dromara.hutool.http.meta.HttpStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class SaTokenConfigure {

    private final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 注册 [Sa-Token全局过滤器]
     */
    @Bean
    public SaReactorFilter getSaReactorFilter() {
        return new SaReactorFilter()
                // 指定 [拦截路由]
                .addInclude("/**")    /* 拦截所有path */
                // 指定 [放行路由]
                .addExclude(
                        "/favicon.ico",
                        "/*/v3/api-docs/**",
                        "/bdq-wechat-miniapp-api/customer/jsCodeLogin",
                        "/bdq-wechat-miniapp-api/config/dict",
                        "/bdq-wechat-miniapp-api/config/keyStr",
                        "/bdq-wechat-miniapp-api/config/city",
                        "/bdq-wechat-miniapp-api/index/banners",
                        "/bdq-wechat-miniapp-api/index/productList",
                        "/bdq-wechat-miniapp-api/index/productHotList",
                        "/bdq-wechat-miniapp-api/index/productTypeList",
                        "/bdq-wechat-miniapp-api/index/productRecommendList",
                        "/bdq-admin-api/user/login",
                        "/bdq-worker-app-api/worker/register",
                        "/bdq-worker-app-api/worker/login",
                        "/bdq-worker-app-api/aliyun/sts/get",
                        "/bdq-worker-app-api/attachment/upload",
                        "/bdq-worker-app-api/config/dict",
                        "/bdq-worker-app-api/config/city",
                        "/bdq-worker-app-api/config/citySelect",
                        "/bdq-worker-app-api/config/appVersion"
                )
                // 指定[认证函数]: 每次请求执行
                .setAuth(obj -> {
//                    String tokenValue = StpUtil.getTokenValue();
//                    StpUtil.checkLogin();
//                    System.out.println("---------- sa全局认证");

                    SaRouter.match("/bdq-admin-api/**", StpAdminUtil::checkLogin);
//
                    SaRouter.match("/bdq-wechat-miniapp-api/**", StpWechatMiniAppUtil::checkLogin);

                    SaRouter.match("/bdq-worker-app-api/**", StpWorkerUtil::checkLogin);

                })
                // 指定[异常处理函数]：每次[认证函数]发生异常时执行此函数
                .setError(e -> {
                    log.error("", e);
                    SaHolder.getResponse().setHeader(HeaderName.CONTENT_TYPE.getValue(), "application/json;charset=UTF-8");
                    return obj2JsonString(Resp.error(HttpStatus.HTTP_UNAUTHORIZED, e.getMessage()));
                });
    }

    @SneakyThrows
    private String obj2JsonString(Object o) {
        return objectMapper.writeValueAsString(o);
    }

}
